<template>
  <div>
    <div class="roomInformation">
      <!-- 搜索 -->
      <el-card class="box-card">
        <div class="ruleForm">
          <el-form size="small" label-width="100px" class="ruleFormContaner">
            <el-form-item label="名称搜索：" class="width100">
              <el-input
                v-model="formData.wifiName"
                placeholder="请输入 wifi 名称"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  @click="seachList"
                />
              </el-input>
            </el-form-item>
          </el-form>
          <div style="margin-left: 16px">
            <el-button
              class="mr10"
              size="small"
              type="primary"
              @click="open('add')"
              >添加wifi</el-button
            >
          </div>
          <!-- 弹窗 -->
          <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose"
          >
            <el-form
              :model="ruleForm"
              :rules="rules"
              ref="ruleForm"
              label-width="120px"
              class="demo-ruleForm"
            >
              <!-- 酒店名称 -->
              <el-form-item label="酒店名称：" prop="jdId">
                <el-select
                  v-model="ruleForm.jdId"
                  clearable
                  placeholder="请选择酒店名称"
                  @change="selectJd($event)"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.jdName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- 无线网络名称 -->
              <el-form-item label="无线网络名称：" prop="wifiName">
                <el-input v-model="ruleForm.wifiName"></el-input>
              </el-form-item>
              <!-- 无线网络密码 -->
              <el-form-item label="无线网络密码：" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
              </el-form-item>
              <div ref="detailButton" v-if="title != '详情'">
                <el-form-item>
                  <el-button type="primary" @click="submitForm('ruleForm')"
                    >保存</el-button
                  >
                  <el-button @click="cancle('ruleForm')">取消</el-button>
                </el-form-item>
              </div>
            </el-form>
          </el-dialog>
        </div>
      </el-card>
      <!-- 列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed prop="jdName" label="酒店名称" align="center">
        </el-table-column>
        <el-table-column prop="wifiName" label="无线网络名称" align="center">
        </el-table-column>
        <el-table-column prop="password" label="无线网络密码" align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="open('detail', scope.row.id)"
              >详情</el-button
            >
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="open('edit', scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="del(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="formData.page"
          :page-sizes="[10, 30, 40, 50]"
          :page-size="formData.limit"
          layout="total,sizes,prev, pager, next,jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  wifiListApi,
  addWifiApi,
  delWifiApi,
  detailWifiApi,
  editWifiApi,
} from "@/api/wifiType";
import { groggeryListApi } from "@/api/roomInformation";
export default {
  data() {
    return {
      rules: {
        //酒店名称
        jdId: [{ required: true, message: "请输入酒店名称", trigger: "blur" }],
        //无线网络名称
        wifiName: [
          { required: true, message: "请输入无线网络名称", trigger: "blur" },
        ],
        //无线网络密码
        password: [
          { required: true, message: "请输入无线网络密码", trigger: "blur" },
        ],
      },
      options: [], //所有酒店名称
      ruleForm: {
        jdName: "", //酒店名称
        wifiName: "", //无线网络名称
        password: "", //无线网络密码
        id: "",
      },
      title: "新增", // 默认标题
      dialogVisible: false, //dialog开关
      tableData: [], // 列表数据
      formData: {
        limit: 10, // 每页数量
        page: 1, // 页码
        wifiName: "", //无线网络名称
        jdId:'' //酒店 id
      },
      total: 0, // 总数据量
      type: "", // dialog 类型
    };
  },
  created() {
    this.loadData(); // 获取wifi列表
    this.gethotalList(); // 获取所有的酒店
  },
  methods: {
    //酒店下拉选择 id 赋值
    selectJd(val) {
      this.ruleForm.jdName = val;
    },
    //dialog关闭
    cancle() {
      this.dialogVisible = false;
    },
    // 获取所有的酒店
    async gethotalList() {
      const res = await groggeryListApi(this.hotalForm);
      this.options = res.list;
    },
    // wifi 搜索
    async seachList() {
      this.formData.page = 1;
      this.loadData(); // 获取wifi列表
    },
    //添加房型
    async open(type, id) {
      this.type = type;
      this.dialogVisible = true;
      if (this.type == "add") {
        this.title = "新增";
        this.ruleForm = {
          jdName: "", //酒店名称
          wifiName: "", //无线网络名称
          password: "", //无线网络密码
          id: 0,
        };
      }
      if (this.type == "detail") {
        this.title = "详情";
        const res = await detailWifiApi({ id: id });
        this.ruleForm = res;
      }
      if (this.type == "edit") {
        this.title = "编辑";
        const res = await detailWifiApi({ id: id });
        this.ruleForm = res;
      }
    },
    //关闭对话框
    handleClose(done) {
      done();
    },
    // 删除
    async del(id) {
      try {
        await this.$confirm("是否删除该wifi", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delWifiApi({ id: id });
        this.$message.success("删除成功");
        this.loadData();
      } catch {}
    },
    // 获取wifi列表
    async loadData() {
      const res = await wifiListApi(this.formData);
      this.tableData = res.list;
      this.total = res.total;
    },
    //分页处理
    handleSizeChange(val) {
      this.formData.limit = val;
      this.loadData();
    },
    //分页处理
    handleCurrentChange(val) {
      this.formData.page = val;
      this.loadData();
    },
    reset() {
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
      this.loadData();
    },
    submitForm(formName) {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        //通过后发请求
        if (this.type == "add") {
          await addWifiApi(this.ruleForm);
          this.$message.success("添加 wifi 成功");
          this.reset();
        }
        if (this.type == "edit") {
          await editWifiApi(this.ruleForm);
          this.$message.success("更改 wifi 成功");
          this.reset();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.roomInformation {
  padding: 20px;
  .box-card {
    margin-bottom: 20px;
    .ruleForm {
      .ruleFormContaner {
        display: flex;
      }
    }
    .el-select {
      width: 15vw;
    }
    .el-input {
      width: 15vw;
    }
  }
  .block {
    padding: 20px;
    text-align: right;
    width: 100%;
    background-color: #ffffff;
  }
}
</style>
